class ScrapeController {

    def index = {
        // info_hash: urlencoded 20-byte SHA1 hash of the value of the info key from the Metainfo file. Note that the value will be a bencoded dictionary, given the definition of the info key above.
        def infoHash = params['info_hash']

        Torrent torrent;
        if ( infoHash && ( torrent = Torrent.findByInfoHash( infoHash ) ) ) {
            //return just the one file
            Map stats = torrent.stats()
            def totalComplete = stats.complete;
            def totalIncomplete = stats.incomplete;

            Map response = [ files:[ infoHash:[
                        complete:totalComplete,
                        incomplete:totalComplete,
                        downloaded:torrent.completed
                    ]]]
            render response.bencode()
            return
        }

        //otherwise return them all
        def torrentList = Torrent.findAll()
        Map files = [:]
        torrentList.each {  
            Map stats = it.stats()
            files[it.infoHash] = [
                complete:stats.complete, 
                incomplete:stats.incomplete,
                downloaded:it.completed
            ]
        }

        Map response = [ 'files':files ]
        render response.bencode()
    }
}
